var NceaMgmt = function(obj, opts) {
  this.__obj = obj;
  this.__student = (opts && opts.student) ? opts.student : {};
  this.__pdenrls = (opts && opts.pdenrls) ? opts.pdenrls : [];
  this.__enrlclasses = (opts && opts.enrlclasses) ? opts.enrlclasses : [];
  this.__enrlintakes = (opts && opts.enrlintakes) ? opts.enrlintakes : [];
  this.__courses = (opts && opts.courses) ? opts.courses : [];
  this.__update_nav = (opts && opts.update_nav) ? opts.update_nav : function(id, eit_id) {};
  this.__nceats = (opts && opts.nceats) ? opts.nceats : [];
  this.__readonly = (opts && opts.readonly == 1) ? opts.readonly : 0;
  this.__new_eits = [];
  this.__intakes = [];
  this.__classes = [];
  
  this.create_extra();
};

NceaMgmt.prototype.create = function() {
  var self = this;
  
  for (var idx in this.__obj.__enrlintakes) {
    var itm = this.__obj.__enrlintakes[idx];
    itm.__class = 0;
    itm.__pass = 0;
    itm.__fail = 0;
    itm.__wtdr = 0;
    for (var idx2 in this.__enrlclasses) {
      var itm2 = this.__enrlclasses[idx2];
      if (itm2.enrolled_intake_id == itm.id) {
        itm.__class++;
        if (itm2.status == 1) {
          itm.__pass++;
        } else if (itm2.status == 2) {
          itm.__fail++;
        } else if (itm2.status == 3) {
          itm.__wtdr++;
        }
      }
    }
  }
  
  $('#ep_content').empty();
  if (this.__readonly == 0) {
    $('#ep_content').append('<button class="pure-button pure-button-primary" onclick="__ncea_m.dialog_open();">Add Term</button>');
  }
  
  $('#ep_content').append(
    '<table style="width: 650px;" class="pure-table pure_table_academic" cellpadding=0 cellspacing=0>' +
      '<thead>' +
        '<tr>' +
          '<th style="width: 90px;">Type</th>' +
          '<th>Term</th>' +
          '<th style="width: 40px;">Weeks</th>' +
          '<th style="width: 40px;">&nbsp;</th>' +
        '</tr>' +
      '</thead>' +
      '<tbody id="eit_body"></tbody>' +
    '</table>'
  );
  this.__obj.__enrlintakes = this.__obj.__enrlintakes.sort(function(a, b) {
    return Utils.to_date(a.start).getTime() > Utils.to_date(b.start).getTime() ? -1 : 1;
  });
  for (var idx in this.__obj.__enrlintakes) {
    var itm = this.__obj.__enrlintakes[idx];
    $('#eit_body').append(
      '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
        '<td>' + itm.type_ncea__ + '</td>' +
        ((itm.type_ncea__ == this.__nceats[0]) ? '<td><input id="eits_' + itm.id + '" style="width: 90px;" type="text" value="' + itm.start + '"/>&nbsp;to&nbsp;<input id="eite_' + itm.id + '" style="width: 90px;" type="text" value="' + itm.end + '"/></td>' : '<td>' + itm.start + ' to ' + itm.end + '</td>') +
        '<td>' + Utils.wks(itm.start, itm.end) + '</td>' +
        '<td>' +
          '<i class="fa fa-pencil" onclick="__ncea_m.create_eit(' + itm.id + ');"></i>&nbsp;' +
          ((this.__readonly == 0) ? '<i class="fa fa-times" onclick="__ncea_m.del_eit(' + itm.id + ');"></i>' : '') +
        '</td>' +
      '</tr>'
    );
    $('#eits_' + itm.id).datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true,
      onSelect: function(dateText) {
        req = Utils.get_by_id(this.id.replace('eits_', ''), self.__obj.__enrlintakes);
        req.start = dateText;
        Ajax.request(
          __requrl + 'submit_enrl_intakes',
          'req=' + Utils.encodeURI(JSON.stringify([ req ])),
          function(rslt) {
            var obj = Utils.get_by_id(req.id, self.__enrlintakes);
            obj.start = dateText;
            self.create.call(self);
          },
          null
        );
      }
    });
    $('#eite_' + itm.id).datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true,
      onSelect: function(dateText) {
        req = Utils.get_by_id(this.id.replace('eite_', ''), self.__obj.__enrlintakes);
        req.end = dateText;
        Ajax.request(
          __requrl + 'submit_enrl_intakes',
          'req=' + Utils.encodeURI(JSON.stringify([ req ])),
          function(rslt) {
            var obj = Utils.get_by_id(req.id, self.__enrlintakes);
            obj.end = dateText;
            self.create.call(self);
          },
          null
        );
      }
    });
  }
  
  if (this.__obj.__enrlintakes.length == 0) {
    $('#eit_body').append('<tr class="pure-table-even"><td colspan=8>None</td></tr>');
  }
  
  this.__update_nav(this.__obj.id, -1);
};

NceaMgmt.prototype.create_eit = function(id) {
  this.__update_nav(this.__obj.id, id);

  var self = this;
  
  var obj = Utils.get_by_id(id, this.__obj.__enrlintakes);
  var req = { 
    id: obj.intake_id
  };
  if (obj.type_ncea__ == this.__nceats[0]) {
    Ajax.request(
      __requrl + 'get_ncea_classes1',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        self.__classes = rslt;
        self.setup_eit.call(self, id);
      },
      null
    );
  } else if (obj.type_ncea__ == this.__nceats[1]) {
    Ajax.request(
      __requrl + 'get_ncea_classes2',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        self.__classes = rslt;
        self.setup_eit.call(self, id);
      },
      null
    );
  } else if (obj.type_ncea__ == this.__nceats[2]) {
    Ajax.request(
      __requrl + 'get_classes2',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        self.__classes = rslt;
        self.setup_eit.call(self, id);
      },
      null
    );
  }
};

NceaMgmt.prototype.del_eit = function(id) {
  var self = this;
  
  var req = {
    id: id
  }

  Ajax.request(
    __requrl + 'delete_enrl_intake',
    'req=' + Utils.encodeURI(JSON.stringify(req)),
    function(rslt) {
      Utils.splice_by_id(id, self.__enrlintakes);
      Utils.splice_by_id(id, self.__obj.__enrlintakes);
      self.__update_nav(self.__obj.id, -1);
      self.create.call(self);
    },
    { confirm: 1 }
  );
};

NceaMgmt.prototype.setup_eit = function(id) {
  var obj = Utils.get_by_id(id, this.__obj.__enrlintakes);
  
  $('#ep_content').empty();
  $('#ep_content').append('<div id="alleits" style="padding: 10px 20px; width: 650px;" class="ui-state-highlight ui-corner-all"></div>');
  $('#alleits').append('<button class="pure-button pure-button-back" style="margin-right: 5px;" onclick="__ncea_m.create()"><i class="fa fa-arrow-circle-left"></i>&nbsp;Back</button>');

  for (var idx in this.__obj.__enrlintakes) {
    var itm = this.__obj.__enrlintakes[idx];
    $('#alleits').append('<button class="pure-button' + ((itm.id == id) ? ' pure-button-intake' : '') + '" style="margin-right: 5px;" onclick="__ncea_m.create_eit(' + itm.id + ')">' + itm.start + ' to ' + itm.end + '</button>');
  }
  
  $('#ep_content').append(
    '<div>&nbsp;</div>' +
    '<div class="pure-form">' +
      '<legend><h3>' + obj.start + ' to ' + obj.end + '</h3></legend>' +
     
      '<fieldset class="pure-group">' +
        '<input' + ((this.__readonly == 1) ? ' disabled="disabled"' : '') + ' id="eit_vw" type="text" class="pure-input-1-2" placeholder="Verbal Warning" value="' + ((obj.w1 == '0000-00-00') ? '' : obj.w1) + '"/>' +
        '<input' + ((this.__readonly == 1) ? ' disabled="disabled"' : '') + ' id="eit_1st" type="text" class="pure-input-1-2" placeholder="1st Warning" value="' + ((obj.w2 == '0000-00-00') ? '' : obj.w2) + '"/>' +
        '<input' + ((this.__readonly == 1) ? ' disabled="disabled"' : '') + ' id="eit_2nd" type="text" class="pure-input-1-2" placeholder="2nd Warning" value="' + ((obj.w3 == '0000-00-00') ? '' : obj.w3) + '"/>' +
        '<textarea id="eit_note" style="resize: none;" class="pure-input-1-2" placeholder="Note">' + (obj.note ? obj.note : '') + '</textarea>' +
      '</fieldset>' +
      '<button class="pure-button pure-input-1-2 pure-button-primary" onclick="__ncea_m.save_eit(' + id + ');">Save</button>' +
    '<div>' + 
    '<div>&nbsp;</div>' +
    '<div id="eit_classes"></div>'
  );
  
  $('#eit_vw').datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "yy-mm-dd",
    showButtonPanel: true
  });
  $('#eit_1st').datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "yy-mm-dd",
    showButtonPanel: true
  });
  $('#eit_2nd').datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "yy-mm-dd",
    showButtonPanel: true
  });
  
  this.create_eit_classes(id);
};


NceaMgmt.prototype.create_eit_classes = function(id) {
  var self = this;
  
  $('#eit_classes').empty();
  $('#eit_classes').append(
    '<table class="pure-table pure_table_academic" cellpadding=0 cellspacing=0>' +
      '<thead>' +
        '<tr>' +
          '<th>Class</th>' +
          '<th style="width: 60px;">Atdn</th>' +
          '<th style="width: 200px;">Status</th>' +
        '</tr>' +
      '</thead>' +
      '<tbody id="ecl_body"></tbody>' +
    '</table>'
  );
  
  var obj = Utils.get_by_id(id, this.__obj.__enrlintakes);
  for (var idx in obj.__enrlclasses) {
    var itm = obj.__enrlclasses[idx];
    $('#ecl_body').append(
      '<tr class="' + ((idx % 2 == 1) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
        '<td><select' + ((itm.__atdn != -1 || this.__readonly == 1) ? ' disabled="disabled"' : '') + ' style="width: 100%;" id="ecl_ecl_' + itm.id + '" onchange="__ncea_m.update_ecl(' + itm.id + ', ' + id + ');"><option value="0">&nbsp;</option></select></td>' +
        '<td>' + itm.__atdn + '%</td>' +
        '<td>' +
          '<select' + ((this.__readonly == 1) ? ' disabled="disabled"' : '') + ' id="ecl_ecl_sts_' + itm.id + '" onchange="__ncea_m.update_ecl_sts(' + itm.id + ', ' + id + ');">' +
            '<option value="0">Normal</opiton>' +
            '<option value="3">Withdrawn</opiton>' +
          '</select>' + 
        '</td>' +
      '</tr>'
    );
    
    $('#ecl_ecl_sts_' + itm.id).val(itm.status);
    if (obj.type_ncea__ == 'Term 0 Class 1') {
      $('#ecl_ecl_sts_' + itm.id).hide();
    }
    
    for (var idx2 in this.__classes) {
      var itm2 = this.__classes[idx2];
      var exist = 0;
      if (itm.class_id != itm2.id) {
        for (var idx3 in obj.__enrlclasses) {
          var itm3 = obj.__enrlclasses[idx3];
          if (itm3.class_id == itm2.id) {
            exist = 1;
          }
        }
      }
      if (exist == 0) {
        $('#ecl_ecl_' + itm.id).append('<option value="' + itm2.id + '"' + ((itm2.id == itm.class_id) ? ' selected="selected"' : '') + '>' + itm2.course_name + ' ' + itm2.name + '</option>');
      }
    }
  }

  if (this.__readonly == 0) {
    for (var i = obj.papers, il = -1; i > il; i--) {
      $('#ecl_body').append(
        '<tr class="' + ((i % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td><select style="width: 100%;" id="ecl_cl_' + i + '" onchange="__ncea_m.add_ecl(' + i + ', ' + id + ');"><option value="0">&nbsp;</option></select></td>' +
          '<td></td>' +
          '<td></td>' +
        '</tr>'
      );
      for (var idx in this.__classes) {
        var itm = this.__classes[idx];
        var exist = 0;
        for (var idx2 in obj.__enrlclasses) {
          var itm2 = obj.__enrlclasses[idx2];
          if (itm.id == itm2.class_id) {
            exist = 1;
          }
        }
        if (exist == 0) {
          $('#ecl_cl_' + i).append('<option value="' + itm.id + '">' + itm.course_name + ' ' + itm.name + '</option>');
        }
      }
    }
  }
  var enrlcl = Utils.get_by_id();
  $('#eit_classes').append(
    '<div>&nbsp;</div>' +
    '<div class="pure-button pure-button-pass" style="margin-bottom: 5px;" onclick="window.open(\'____tb.php?id=' + id + '\')">Print Timetable</div>' +
    '<div id="timetable"></div>'
  );
  
  $('#timetable').empty();
  $('#timetable').fullCalendar({
    defaultView: 'agendaWeek',
    header: {
      left: '',
      center: '',
      right: ''
    },
    columnFormat: {
      week: 'ddd'
    },
    year: 2000,
    month: 0,
    date: 3,
    allDaySlot: false,
    height: 800,
    weekends: false,
    theme: true,
    
    minTime: 8,
    maxTime: 23,
    
    timeFormat: 'HH:mm{-HH:mm}',

    editable: false,
    selectable: false,

    events: function(start, end, callback) {
      var obj = Utils.get_by_id(id, self.__obj.__enrlintakes);
      for(var idx in obj.__enrlclasses) {
        var itm = obj.__enrlclasses[idx];
        if (itm.status == 3) {
          continue;
        }
        for (var idx2 in JSON.parse(itm.__times)) {
          var itm2 = JSON.parse(itm.__times)[idx2];
          var cl = Utils.get_by_id(itm.class_id, self.__classes);
          if (itm.type__ == 'Language') {
            var rslt = itm2.split('-');
            for (var i = 3; i < 8; i++) {
              var evt = {
                start: '2000-01-0' + i + ' ' + rslt[0] + ':00',
                end: '2000-01-0' + i + ' ' + rslt[1] + ':00',
                allDay: 0,
                title: itm.class_name + ((cl.room_id != 0) ? '\n' + cl.room_name : '') + ((cl.teacher_id != 0) ? '\n' + cl.teacher_name : ''),
                description: ''
              };
              $("#timetable").fullCalendar('renderEvent', evt, true);
            }
          } else {
            var rslt = itm2.split('#');
            var evt = {
              start: rslt[0] + ':00',
              end: rslt[1] + ':00',
              allDay: 0,
              title: itm.course_name + ' ' + itm.class_name + ((cl.room_id != 0) ? '\n' + cl.room_name : '') + ((cl.teacher_id != 0) ? '\n' + cl.teacher_name : ''),
              description: ''
            };
            $("#timetable").fullCalendar('renderEvent', evt, true);
          }
        }
      }
    }
  });
  
};


NceaMgmt.prototype.update_ecl_sts = function(ecl_id, eit_id) {
  var self = this;
  
  var eit = Utils.get_by_id(eit_id, this.__obj.__enrlintakes);
  var ecl = Utils.get_by_id(ecl_id, eit.__enrlclasses);
  var ecl2 = Utils.get_by_id(ecl_id, this.__enrlclasses);
  ecl.status = $('#ecl_ecl_sts_' + ecl_id + ' :selected').val();
  ecl2.status = $('#ecl_ecl_sts_' + ecl_id + ' :selected').val();
  Ajax.request(
    __requrl + 'submit_enrl_class',
    'req=' + Utils.encodeURI(JSON.stringify(ecl)),
    function(rslt) {
      self.create_eit_classes.call(self, eit_id);
      self.create_extra.call(self);
    },
    null
  );
};

NceaMgmt.prototype.update_ecl = function(ecl_id, eit_id) {
  var self = this;
  
  var cl = Utils.get_by_id($('#ecl_ecl_' + ecl_id + ' :selected').val(), this.__classes);
  var eit = Utils.get_by_id(eit_id, this.__obj.__enrlintakes);
  var ecl = Utils.get_by_id(ecl_id, eit.__enrlclasses);
  var ecl2 = Utils.get_by_id(ecl_id, this.__enrlclasses);
  
  if (!cl) {
    
    Ajax.request(
      __requrl + 'delete_enrl_class',
      'req=' + Utils.encodeURI(JSON.stringify(ecl)),
      function(rslt) {
        Utils.splice_by_id(ecl_id, this.__enrlclasses);
        Utils.splice_by_id(ecl_id, eit.__enrlclasses);
        self.create_eit_classes.call(self, eit_id);
        self.create_extra.call(self);
      },
      null
    );
  } else {
    ecl.class_id = cl.id;
    ecl.class_name = cl.name;
    ecl.course_id = cl.course_id;
    ecl.course_name = cl.course_name;
    ecl.type__ = cl.type;
    ecl.__times = cl.times;
    ecl2.class_id = cl.id;
    ecl2.class_name = cl.name;
    ecl2.course_id = cl.course_id;
    ecl2.course_name = cl.course_name;
    ecl.type__ = cl.type;
    ecl2.__times = cl.times;
    Ajax.request(
      __requrl + 'submit_enrl_class',
      'req=' + Utils.encodeURI(JSON.stringify(ecl)),
      function(rslt) {
        self.create_eit_classes.call(self, eit_id);
        self.create_extra.call(self);
      },
      null
    );
  }
};

NceaMgmt.prototype.add_ecl = function(idx, id) {
  var self = this;
  
  var obj = Utils.get_by_id($('#ecl_cl_' + idx + ' :selected').val(), this.__classes);
  var obj2 = Utils.get_by_id(id, this.__obj.__enrlintakes);
  var req = {
    type__: obj.type,
    type_ncea__: obj2.type_ncea__,
    enrollment_id: obj2.enrollment_id,
    enrolled_program_id: obj2.enrolled_program_id,
    enrolled_intake_id: id,
    intake_id: 0,
    class_id: obj.id,
    class_name: obj.name,
    course_id: obj.course_id,
    course_name: obj.course_name,
    student_id: this.__student.id,
    start: obj2.start,
    end: obj2.end,
    status: 0,
    result__: -2,
    __times: obj.times,
    __atdn: -1
  };
  Ajax.request(
    __requrl + 'submit_enrl_class',
    'req=' + Utils.encodeURI(JSON.stringify(req)),
    function(rslt) {
      self.__enrlclasses.push(rslt);
      var obj = Utils.get_by_id(id, self.__obj.__enrlintakes);
      obj.__enrlclasses.push(rslt);
      self.create_eit_classes.call(self, id);
      self.create_extra.call(self);
    },
    null
  );
};

NceaMgmt.prototype.save_eit = function(id) {
  var self = this;
  
  var obj = Utils.get_by_id(id, this.__obj.__enrlintakes);
  obj.w1 = ($('#eit_vw').val() == '') ? '0000-00-00' : $('#eit_vw').val();
  obj.w2 = ($('#eit_1st').val() == '') ? '0000-00-00' : $('#eit_1st').val();
  obj.w3 = ($('#eit_2nd').val() == '') ? '0000-00-00' : $('#eit_2nd').val();
  obj.note = $('#eit_note').val();
  Ajax.request(
    __requrl + 'submit_enrl_intakes',
    'req=' + Utils.encodeURI(JSON.stringify([ obj ])),
    function(rslt) {
      self.create_eit_classes.call(self, id);
    },
    null
  );
};

NceaMgmt.prototype.dialog_open = function() {
  var self = this;
  var req = {
    type: this.__obj.program_type
  }
  Ajax.request(
    __requrl + 'get_intakes',
    'req=' + Utils.encodeURI(JSON.stringify(req)),
    function(rslt) {
      self.__intakes = rslt;
      self.__new_eits = [];
      $('#dialog_academic').empty();
      $('#dialog_academic').append(
        '<div class="pure-form">' +
            '<b>Enroll for</b>&nbsp;<select id="pdenrls" class="pure-input-1-2" style="width: 400px;"></select>' +
        '</div>' +
        '<div id="enrlitks" style="margin-top: 5px; border: 1px solid #FFFF00; padding: 5px 10px; width: 300px; background: #FFFFCC;"></div>' +
        '<div style="border-bottom: 1px solid #666;">&nbsp;</div>' +
        '<div>&nbsp;</div>' +
        '<div id="dialog_academic_content">&nbsp;</div>'
      );
      for (var idx in self.__pdenrls) {
        var itm = self.__pdenrls[idx];
        if (itm.enrolled_program_id != self.__obj.id) {
          continue;
        }
        $('#pdenrls').append('<option value="' + itm.id + '">' + (itm.is_enrolled == 1 ? 'Enrolled' : 'Paid') + ', ' + itm.start_academic + ' to ' + itm.end_academic + ', ' + itm.program_name + '</option>');
      }
      
      self.dialog_setup.call(self);
    },
    null
  );
};

NceaMgmt.prototype.dialog_setup = function() {
  var self = this;
  
  var func = function() {
    Ajax.request(
      __requrl + 'submit_enrl_intakes',
      'req=' + Utils.encodeURI(JSON.stringify(self.__new_eits)),
      function(rslt) {
        var enrl = Utils.get_by_id($('#pdenrls :selected').val(), __pdenrls);
        enrl.is_enrolled = 1;
        self.__enrlintakes = self.__enrlintakes.concat(rslt);
        self.__obj.__enrlintakes = self.__obj.__enrlintakes.concat(rslt);
        self.create.call(self);
        $('#dialog_academic').dialog('close');
      },
      null
    );
  };
  
  $('#dialog_academic').dialog({
    width: 500,
    buttons: {
      'OK': function() {
        func();
      },
      'Cancel': function() { 
        $(this).dialog('close');
      }
    }
  });
  
  $('#dialog_academic_content').empty();
  $('#dialog_academic_content').append(
    '<div class="pure-form">' +
      '<select id="intaketps" style="margin-right: 3px;" onchange="__ncea_m.dialog_render_terms();"></select>' +
      '<select id="intakeopts" class="pure-input-1-2" style="margin-right: 3px;"></select>' +
      '<button class="pure-button pure-primary" onclick="__ncea_m.dialog_add_eit();">Add</button>' +
    '</div>'
  );
  
  for (var idx in this.__nceats) {
    var itm = this.__nceats[idx];
    $('#intaketps').append('<option value="' + itm + '">' + itm + '</option>');
  }
  
  this.dialog_render_terms();
  
  $('#dialog_academic_content').append(
    '<table class="pure-table pure_table_academic" cellpadding=0 cellspacing=0>' +
      '<thead>' +
        '<tr>' +
          '<th style="width: 90px;">Type</th>' +
          '<th>Term</th>' +
          '<th style="width: 30px;">Weeks</th>' +
          '<th style="width: 40px;">&nbsp;</th>' +
        '</tr>' +
      '</thead>' +
      '<tbody id="newenrlintakebody"></tbody>' +
    '</table>'
  );
  
  this.__new_eits = this.__new_eits.sort(function(a, b) {
    return a.start < b.start;
  });
  for (var idx in this.__new_eits) {
    var itm = this.__new_eits[idx];
    $('#newenrlintakebody').append(
      '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
        '<td>' + itm.type_ncea__ + '</td>' +
        ((itm.type_ncea__ == this.__nceats[0]) ? '<td><input id="neits_' + idx + '" style="width: 90px;" type="text" value="' + itm.start + '"/>&nbsp;to&nbsp;<input id="eite_' + idx + '" style="width: 90px;" type="text" value="' + itm.end + '"/></td>' : '<td>' + itm.start + ' to ' + itm.end + '</td>') +
        '<td>' + Utils.wks(itm.start, itm.end) + '</td>' +
        '<td><i class="fa fa-times" onclick="__ncea_m.dialog_del_eit(' + idx + ');"></i></td>' +
      '</tr>'
    );
    $('#neits_' + idx).datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true,
      onSelect: function(dateText) {
        self.dialog_upt_eit.call(self, idx, 'start', dateText);
      }
    });
    $('#neite_' + idx).datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true,
      onSelect: function(dateText) {
        self.dialog_upt_eit.call(self, idx, 'start', dateText);
      }
    });
  }
  if (this.__new_eits.length == 0) {
    $('#newenrlintakebody').append('<tr class="pure-table-even"><td colspan=4>None</td></tr>');
  }
  
  var enrl = Utils.get_by_id($('#pdenrls :selected').val(), this.__pdenrls);
  $('#enrlitks').empty();
  var count = 0;
  var enrlitks = Utils.clone(this.__intakes).reverse();
  for (var idx in enrlitks) {
    var itm = enrlitks[idx];
    if (enrl.intakes__.indexOf(',' + itm.id + ',') == -1) {
      continue;
    }
    count++;
    $('#enrlitks').append('<div style="padding: 2px 0;"><b>Intake ' + count + ':</b> ' + ((itm.type_ncea != '') ? '(' + itm.type_ncea + ') ' : '') + itm.start + ' to ' + itm.end + '</div>');
  }
};

NceaMgmt.prototype.dialog_render_terms = function() {
  if ($('#intaketps :selected').val() == this.__nceats[0]) {
    $('#intakeopts').hide();
  } else {
    $('#intakeopts').show();
  }
  
  $('#intakeopts').empty();
  for (var idx in this.__intakes) {
    var itm = this.__intakes[idx];
    if ($('#intaketps :selected').val() != itm.type_ncea) {
      continue;
    }
    $('#intakeopts').append('<option ' + ((this.dialog_chk_it(itm)) ? '' : 'disabled="disabled" ') + 'value="' + itm.id + '">' + itm.start + ' to ' + itm.end + ((this.dialog_chk_it(itm)) ? '' : ' (Enrolled)') + '</option>');
  }
};

NceaMgmt.prototype.dialog_upt_eit = function(idx, attr, date) {
  this.__new_eits[idx][attr] = date
};

NceaMgmt.prototype.dialog_chk_it = function(obj) {
  for (var idx in this.__new_eits) {
    var itm = this.__new_eits[idx];
    if (itm.intake_id == obj.id) {
      return false;
    }
  }
  for (var idx in this.__enrlintakes) {
    var itm = this.__enrlintakes[idx];
    if (itm.intake_id == obj.id) {
      return false;
    }
  }
  return true;
};

NceaMgmt.prototype.dialog_add_eit = function() {
  var it = Utils.get_by_id($('#intakeopts :selected').val(), this.__intakes);
  if (!it && $('#intaketps :selected').val() != this.__nceats[0]) {
    return;
  }
  
  var pe = Utils.get_by_id($('#pdenrls :selected').val(), this.__pdenrls);
  var obj = {
    type__: __ncea,
    type_ncea__: $('#intaketps :selected').val(),
    enrollment_id: pe.id,
    enrolled_program_id: this.__obj.id,
    student_id: this.__student.id,
    intake_id: 0,
    start: pe.start_academic,
    end: pe.end_academic,
    papers: 0,
    w1: '0000-00-00',
    w2: '0000-00-00',
    w3: '0000-00-00',
    note: '',
    __enrlclasses: []
  };
  if (it) {
    obj.intake_id = it.id;
    obj.start = it.start;
    obj.end = it.end;
  }
  
  this.__new_eits.push(obj);
  this.dialog_setup();
};

NceaMgmt.prototype.dialog_del_eit = function(idx) {
  this.__new_eits.splice(idx, 1);
  this.dialog_setup();
};

NceaMgmt.prototype.create_extra = function(obj) {
  $('#ep_extra').empty();
  $('#ep_extra').append('<div id="reqs"></div>');
  for (var idx in this.__obj.__reqs) {
    var itm = this.__obj.__reqs[idx];
    $('#reqs').append('<table id="req_' + idx + '" class="pure-table pure_table_academic">');
    $('#req_' + idx).append('<tr class="pure-table-odd"><td>Require ' + itm.num + ' from...</td></tr>');
    $('#req_' + idx).append('<tr class="pure-table-even"><td id="req_courses_' + idx + '"></td></tr>');
    
    for (var idx2 in itm.courses) {
      var itm2 = itm.courses[idx2];
      $('#req_courses_' + idx).append('<button id="req_cs_' + idx2 + '" class="pure-button" style="margin-right: 5px; margin-bottom: 5px;" title="' + this.to_name(itm2) + '">' + Utils.get_short(this.to_name(itm2), 3) + '</button>');
      var rslt = this.to_status(itm2);
      if (rslt == 0) {
        $('#req_cs_' + idx2).addClass('pure-button-enrl');
      } else if (rslt == 1) {
        $('#req_cs_' + idx2).addClass('pure-button-pass');
      } else if (rslt == 2) {
        $('#req_cs_' + idx2).addClass('pure-button-fail');
      }
    }
    
    $('#reqs').append('</table>');
    $('#reqs').append('<br>')
  }
}

NceaMgmt.prototype.to_name = function(obj) {
  var rslt = Utils.get_by_id(obj.id, this.__courses);
  if (obj.alias.length == 0) {
    return rslt.title__;
  }
  var str = rslt.code;
  for (idx in obj.alias) {
    var itm = obj.alias[idx];
    rslt = Utils.get_by_id(itm, this.__courses);
    str += ' or ' + rslt.code;
  }
  return str;
};

NceaMgmt.prototype.to_status = function(obj) {
  for (idx in this.__enrlclasses) {
    var itm = this.__enrlclasses[idx];
    if ((itm.course_id == obj.id || $.inArray(itm.course_id, obj.alias) != -1) && itm.status == 1) {
      return 1;
    }
  }
  
  for (idx in this.__enrlclasses) {
    var itm = this.__enrlclasses[idx];
    if ((itm.course_id == obj.id || $.inArray(itm.course_id, obj.alias) != -1) && itm.status == 0) {
      return 0;
    }
  }
  
  for (idx in this.__enrlclasses) {
    var itm = this.__enrlclasses[idx];
    if ((itm.course_id == obj.id || $.inArray(itm.course_id, obj.alias) != -1) && itm.status == 2) {
      return 2;
    }
  }
  
  return -1;
};


